package com.mornd.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mornd.gulimall.product.entity.Brand;
import com.mornd.gulimall.product.entity.CategoryBrandRelation;
import com.mornd.gulimall.product.mapper.BrandMapper;
import com.mornd.gulimall.product.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mornd.gulimall.product.service.ICategoryBrandRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.springframework.util.StringUtils.hasText;

/**
 * <p>
 * 品牌 服务实现类
 * </p>
 *
 * @author mornd
 * @since 2023-07-23
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {
    @Resource
    private ICategoryBrandRelationService categoryBrandRelationService;

    @Override
    public IPage<Brand> pageList(Brand brand) {
        IPage<Brand> ipage = new Page<>(brand.validPageNum(), brand.validPageSize());
        LambdaQueryWrapper<Brand> qw = Wrappers.<Brand>lambdaQuery().like(
                hasText(brand.getName()), Brand::getName, brand.getName());
        super.page(ipage, qw);
        return ipage;
    }

    @Override
    public boolean existsName(Long id, String name) {
        LambdaQueryWrapper<Brand> qw = Wrappers.<Brand>lambdaQuery().eq(Brand::getName, name);
        qw.ne(id != null, Brand::getBrandId, id);
        long count = super.count(qw);
        return count > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCascade(Brand brand) {
        super.updateById(brand);
        categoryBrandRelationService.updateBrand(brand);
    }

    /**
     * 根据分类id查询品牌
     *
     * @param catId
     * @return
     */
    @Override
    public List<Brand> listByCatId(Long catId) {
        List<CategoryBrandRelation> list = categoryBrandRelationService.list(Wrappers.<CategoryBrandRelation>lambdaQuery().eq(CategoryBrandRelation::getCatelogId, catId));
        List<Long> ids = list.stream().map(CategoryBrandRelation::getBrandId).collect(Collectors.toList());
        if (ids.isEmpty()) {
            return Collections.emptyList();
        }
        return super.list(Wrappers.<Brand>lambdaQuery().in(Brand::getBrandId, ids));
    }

    /**
     * 根据品牌id查询名称
     * @param brandId
     * @return
     */
    @Override
    public String getNameById(Long brandId) {
        if (brandId == null) {
            return null;
        }
        Brand brand = super.getOne(Wrappers.<Brand>lambdaQuery().eq(Brand::getBrandId, brandId).select(Brand::getName));
        return brand == null ? null : brand.getName();
    }
}
